package gzx.specialcomponents
{
	import gzx.specialevents.RelayInfoItemClickEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextLineMetrics;
	
	import mx.controls.Label;
	import mx.core.IFlexModuleFactory;
	import mx.core.UIComponent;
	import mx.events.ResizeEvent;
	import mx.styles.CSSStyleDeclaration;
	
	import spark.components.Group;
	import spark.primitives.BitmapImage;
	
	[Event(name="ItemClick", type="gzx.specialevents.RelayInfoItemClickEvent")]
	[Event(name="ItemDoubleClick", type="gzx.specialevents.RelayInfoItemClickEvent")]
	public class ContinueDiagram extends Group
	{
		[Embed("assets/icon_ms.gif")]
		private var image1:Class;
		[Embed("assets/icon_bts.gif")]
		private var image2:Class;
		[Embed("assets/icon_bsc.gif")]
		private var image3:Class;
		[Embed("assets/icon_msc.gif")]
		private var image4:Class;
		private var relayInfos:Array = [];
		private var terminalEntitys:Array = [{name:"MS", image:image1},{name:"基站",image:image2},{name:"BSC",image:image3},{name:"MSC",image:image4}];
		private var selectedRelayInfo:RelayInfo = null;
		private var actualheight:int = 600;
		private var actualwidth:int = 800;
		private var padTop:int = 80;
		private var padLeft:int = 30;
		private var padRight:int = 30;
		private var heightInterval:int = 40;
		private var entityIndex:int = 1;
		private var eachEntityGap:int = 80;
		private var __moduleFactoryInitialized:Boolean = false;
		
		public function ContinueDiagram()
		{
			super();
			this.doubleClickEnabled = true;
			this.minWidth = 200;
			this.minHeight = 200;
			this.percentWidth = 100.0;
			this.percentHeight = 100.0;
			
			this.addEventListener("resize", this_resizeHandler);
			this.addEventListener("click", this_clickHandler);
			this.addEventListener("doubleClick", this_clickHandler);
			this.addEventListener("creationComplete", init);
		}
		
		override public function set moduleFactory(factory:IFlexModuleFactory):void
		{
			super.moduleFactory = factory;
			if (__moduleFactoryInitialized)
				return;
			__moduleFactoryInitialized = true;
			
			if (!this.styleDeclaration)
			{
				this.styleDeclaration = new CSSStyleDeclaration(null, styleManager);
			}
			
			this.styleDeclaration.defaultFactory = function():void
			{
				this.fontFamily = "微软雅黑";
			};
		}
		
		public function get SelectedRelayInfo():RelayInfo
		{
			return selectedRelayInfo;
		}
		//全图通过relayid关联
		public function SelectedRelayByID(value:int):RelayInfo
		{
			var i:int =0;
			var length:int = relayInfos.length;
			for (i;i<length;i++)
			{
				if(relayInfos[i].RelayID == value)
				{
					selectedRelayInfo = relayInfos[i];
					drawRect(selectedRelayInfo.x, selectedRelayInfo.y);
					return selectedRelayInfo;
					break;
				}
			}
			return null;
		}
		
		public function set RelayInfos(value:Array):void
		{
			this.relayInfos = value;
			selectedRelayInfo = null;
			rectElement = null;
			drawLegend();
			drawRelay();
		}
		
		private function init(event:Event):void
		{
			drawLegend();
		}
		
		private function drawRelay():void
		{
			var count:int = 0;
			var height:int = padTop - heightInterval / 2;
			eachEntityGap = (actualwidth - padLeft - padRight) / 3;
			for each(var relayInfo:RelayInfo in relayInfos)
			{
				if(relayInfo.Direction == RelayInfo.Unknown)
				{
					continue;
				}
				height += heightInterval;
				var leftAt:int = padLeft + eachEntityGap * entityIndex;
				var rightAt:int = padLeft + eachEntityGap * (entityIndex + 1);
				var middleAt:int = (leftAt + rightAt) / 2;
				
				if (relayInfo.Direction == RelayInfo.Right)
				{
					var arrowheadRight:UIComponent = new UIComponent();
					if(relayInfo.Color == -1)
					{
						arrowheadRight.graphics.lineStyle(2,0x00FF00);
					}else{
						arrowheadRight.graphics.lineStyle(2,relayInfo.Color);
					}
					arrowheadRight.graphics.moveTo(leftAt, height);
					arrowheadRight.graphics.lineTo(middleAt, height);
					arrowheadRight.graphics.moveTo(middleAt - 8, height - 4);
					arrowheadRight.graphics.lineTo(middleAt, height);
					arrowheadRight.graphics.lineTo(middleAt - 8, height + 4);
					this.addElement(arrowheadRight);
					
					relayInfo.x = leftAt;/////////记录X、Y轴位置
					relayInfo.y = height-heightInterval/2;/////////
					
					//===
					if (relayInfo.UpString != null)
					{
						var upTextRight:String = relayInfo.UpString;
						if (relayInfo.ShortName != "")
						{
							upTextRight = relayInfo.ShortName;
						}
						var upLabelRight:Label = new Label();
						upLabelRight.text = upTextRight;
						upLabelRight.setStyle("fontSize",10);
						var sizeFUpRight:TextLineMetrics = measureText(relayInfo.UpString);
						upLabelRight.x =  (leftAt + middleAt) / 2 - sizeFUpRight.width / 2;
						upLabelRight.y = height - sizeFUpRight.height - 1;
						this.addElement(upLabelRight);
					}
					if (relayInfo.DownString != null)
					{
						var downLabelRight:Label = new Label();
						downLabelRight.text = relayInfo.DownString;
						downLabelRight.setStyle("fontSize",8);
						var sizeFDownRight:TextLineMetrics = measureText(relayInfo.DownString);
						downLabelRight.x =  (leftAt + middleAt) / 2 - sizeFDownRight.width*0.4;
						downLabelRight.y = height + 1;
						this.addElement(downLabelRight);
					}
				}
				else
				{
					var arrowheadLeft:UIComponent = new UIComponent();
					if(relayInfo.Color == -1)
					{
						arrowheadLeft.graphics.lineStyle(2,0x00FF00);
					}else{
						arrowheadLeft.graphics.lineStyle(2,relayInfo.Color);
					}
					arrowheadLeft.graphics.moveTo(middleAt, height);
					arrowheadLeft.graphics.lineTo(rightAt, height);
					arrowheadLeft.graphics.moveTo(middleAt + 8, height - 4);
					arrowheadLeft.graphics.lineTo(middleAt, height);
					arrowheadLeft.graphics.lineTo(middleAt + 8, height + 4);
					this.addElement(arrowheadLeft);
					
					relayInfo.x = middleAt;/////////记录X、Y轴位置
					relayInfo.y = height-heightInterval/2;/////////
					
					//===
					if (relayInfo.UpString != null)
					{
						var upTextLeft:String = relayInfo.UpString;
						if (relayInfo.ShortName != "")
						{
							upTextLeft = relayInfo.ShortName;
						}
						var upLabelLeft:Label = new Label();
						upLabelLeft.text = upTextLeft;
						upLabelLeft.setStyle("fontSize",10);
						var sizeFUp:TextLineMetrics = measureText(relayInfo.UpString);
						upLabelLeft.x =  (middleAt + rightAt) / 2 - sizeFUp.width / 2;
						upLabelLeft.y = height - sizeFUp.height - 1;
						this.addElement(upLabelLeft);
					}
					if (relayInfo.DownString != null)
					{
						var downLabelLeft:Label = new Label();
						downLabelLeft.text = relayInfo.DownString;
						downLabelLeft.setStyle("fontSize",8);
						var sizeFDown:TextLineMetrics = measureText(relayInfo.DownString);
						downLabelLeft.x =  (middleAt + rightAt) / 2 - sizeFDown.width*0.4;
						downLabelLeft.y = height + 1;
						this.addElement(downLabelLeft);
					}
				}
			}
			if(selectedRelayInfo !=null)
			{
				drawRect(selectedRelayInfo.x,selectedRelayInfo.y);
			}
		}
		
		private function drawLegend():void
		{
			this.removeAllElements();
			actualheight = this.height;
			actualwidth = this.width;
			if(relayInfos!=null && relayInfos.length>0)
			{
				actualheight = padTop + relayInfos.length*heightInterval;
				if(actualheight > this.height)
				{
					actualheight + 10;
					actualwidth = this.width - 25;
				}
			}
			var eachEntityGap:int = (actualwidth - padLeft - padRight) / 3;
			for (var c:int = 0; c < 4; c++)
			{
				var entitySet:Object = terminalEntitys[c];
				var logoMiddleX:int = padLeft + eachEntityGap * c;
				var bitMap:BitmapImage = new BitmapImage();
				bitMap.source = entitySet.image;
				bitMap.x = logoMiddleX - 30;
				bitMap.y = 0;
				bitMap.width = 60;
				bitMap.height = 60;
				this.addElement(bitMap);
				
				var label:Label = new Label();
				label.text = entitySet.name;
				label.setStyle("fontSize",12);
				label.setStyle("fontWeight","bold");
				label.setStyle("color",0x0000FF);
				var sizeF:TextLineMetrics = measureText(entitySet.name);
				label.x = logoMiddleX - sizeF.width*0.6;
				label.y = padTop - sizeF.height - 1;
				this.addElement(label);
				
				var line:UIComponent = new  UIComponent();
				line.graphics.moveTo(logoMiddleX,padTop)
				line.graphics.lineStyle(2,0x0000FF);
				line.graphics.lineTo(logoMiddleX,actualheight);
				this.addElement(line);
			}
			for (var cc:int = 0; cc < 3; cc++)
			{
				var centerLineX:int = padLeft + eachEntityGap * cc + eachEntityGap / 2;
				var dotline:UIComponent = new  UIComponent();
				var time:int = actualheight/32 - 1;
				dotline.graphics.lineStyle(2,0xC89600);
				for(var i:int=0;i<time;i++){
					dotline.graphics.moveTo(centerLineX,padTop + i*32);
					dotline.graphics.lineTo(centerLineX,padTop + 12 + i*32);
					dotline.graphics.moveTo(centerLineX,padTop + 16 + i*32);
					dotline.graphics.lineTo(centerLineX,padTop + 20 + i*32);
					dotline.graphics.moveTo(centerLineX,padTop + 24 + i*32);
					dotline.graphics.lineTo(centerLineX,padTop + 28 + i*32);
				}
				this.addElement(dotline);
			}
		}
		
		private function this_clickHandler(event:MouseEvent):void
		{
			var selX:int = -100;
			var selY:int = -100;
			if(event.target == this)
			{
				selX = event.localX;
				selY = event.localY;
			}else{
				var localPoint:Point = this.globalToLocal(new Point(event.stageX,event.stageY));
				selX = localPoint.x;
				selY = localPoint.y;
			}
			var selRelayInfo:RelayInfo;
			for (var i:int=0;i<relayInfos.length;i++)
			{
				var relayInfo:RelayInfo = relayInfos[i] as RelayInfo;
				if(relayInfo.x<selX && (relayInfo.x+eachEntityGap/2)>selX && relayInfo.y<selY && (relayInfo.y+heightInterval)>selY)
				{
					selRelayInfo = relayInfo;
					break;
				}
			}
			if(selRelayInfo!=null)
			{
				selectedRelayInfo = selRelayInfo;
				var clickType:String = "ItemClick";
				if(event.type =="click")
				{
					drawRect(selRelayInfo.x, selRelayInfo.y);
				}
				else
				{
					clickType = "ItemDoubleClick";
				}
				var eventObject:RelayInfoItemClickEvent = new RelayInfoItemClickEvent(clickType,selRelayInfo);
				dispatchEvent(eventObject);
			}
		}
		private var rectElement:UIComponent;
		private function drawRect(x:int,y:int):void
		{
			if(rectElement!=null)
			{
				try
				{
					(rectElement.parent as Group).removeElement(rectElement);
				}
				catch(e:Error){}
				rectElement = null;
			}
			var selectedRect:UIComponent = new UIComponent();
			selectedRect.graphics.lineStyle(1,0xFF0000);
			selectedRect.graphics.moveTo(x, y);
			selectedRect.graphics.lineTo(x+eachEntityGap/2-1, y);
			selectedRect.graphics.lineTo(x+eachEntityGap/2-1, y+heightInterval);
			selectedRect.graphics.lineTo(x, y+heightInterval);
			selectedRect.graphics.lineTo(x, y);
			this.addElement(selectedRect);
			rectElement = selectedRect;
		}
		
		protected function this_resizeHandler(event:ResizeEvent):void
		{
			if(this.stage!=null)
			{
				rectElement = null;
				drawLegend();
				drawRelay();
			}
		}
	}
}
